package com.bridgeintelligent.tag.customer.service;

import com.bridgeintelligent.tag.customer.entity.CustomersPage;
import com.bridgeintelligent.tag.customer.entity.Issue;
import com.bridgeintelligent.tag.customer.mapper.CustomersMapper;
import com.bridgeintelligent.tag.customer.mapper.IssueMapper;
import com.bridgeintelligent.tag.customer.model.IssueModel;
import com.bridgeintelligent.tag.customer.model.IssueQueryModel;
import com.bridgeintelligent.tag.customer.pojo.Customers;
import com.bridgeintelligent.tag.customer.vo.IssueVO;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.IdWorker;
import com.bridgeintelligent.tag.webserver.security.TagSecurityHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 * @author: grp 2021-02-23 14:48
 **/
@Service
@Slf4j
@AllArgsConstructor
public class IssueServiceImpl implements IIssueService{

    private final IssueMapper issueMapper;
    private final IIssueApproveService issueApproveService;
    private CustomersMapper customersMapper;

    @Override
    public int countById(String id) {
        return issueMapper.countById(id);
    }

    @Override
    public int countByName(String name) {
        return issueMapper.countByName(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(IssueModel model) {
        Issue issue = new Issue();
        BeanUtils.copyProperties(model, issue);
        issue.setId(IdWorker.nextId());
        model.setId(issue.getId());
        issue.setIsDel("00");
        issue.setState("02");
        issue.setApproveStatus("1");
        User user = TagSecurityHelper.currentUser();
        issue.setCreatorId(user.getUserId());
        issue.setCreateTime(new Date());
        issue.setUpdateTime(issue.getCreateTime());
        issueMapper.add(issue);
        //提交审核
        issueApproveService.addCommit(model);
    }

    @Override
    public int countByNameAndNotId(String id, String name) {
        return issueMapper.countByNameAndNotId(name,id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void put(IssueModel model) {
        Issue issue = new Issue();
        BeanUtils.copyProperties(model, issue);
        issue.setUpdateTime(new Date());
        issue.setApproveStatus("2");
        issueMapper.update(issue);
        //提交审核
        try{
            issueApproveService.resubmit(model.getId());
        }catch (Exception e){
            log.info(e.getMessage(),model.getId());
            log.error("重新提交审核失败",e);
        }
    }

    @Override
    public CustomersPage<IssueVO> list(IssueQueryModel model) {
        if (StringUtils.isNotEmpty(model.getStart())) {
            model.setStart(model.getStart() + " 00:00:00");
        }
        if (StringUtils.isNotEmpty(model.getEnd())) {
            model.setEnd(model.getEnd() + " 23:59:59");
        }
        Page page = PageHelper.startPage(model.getPageNumber(), model.getPageSize());
        List<IssueVO> list = issueMapper.findList(model);
        return new CustomersPage<IssueVO>().setContent(list).setTotalPages(page.getPages()).setRequestPager(model.getPageNumber())
                .setSize(model.getPageSize()).setTotalElements(page.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(String id) {
        Issue issue = new Issue();
        issue.setId(id);
        issue.setUpdateTime(new Date());
        issue.setIsDel("01");
        issueMapper.update(issue);
    }

    @Override
    public IssueVO getDetail(String id) {
        IssueVO issueVO = issueMapper.findOne(id);
        if (issueVO != null && !StringUtils.isEmpty(issueVO.getFields())) {
            //处理部分字段，方便前端回显
            Map<String, String> valueMap = new HashMap<>();
            String[] values = issueVO.getFields()
                    .split(",");
            for (String dimId : values) {
                valueMap.put(dimId, customersMapper.findNameByDimId(dimId));
            }
            issueVO.setMap(valueMap);
        }
        return issueVO;

    }

    @Override
    public List<Customers> findCustomers() {
        return issueMapper.findCustomers();
    }

    @Override
    public void changState(IssueModel model) {
        issueMapper.update(new Issue().setState(model.getState()).setId(model.getId()).setUpdateTime(new Date()).setWeeks(model.getWeeks()));
    }
}
